package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.jcy.JcyPrdjConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyJcyJpjbxx;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyJcyJpjbxxMapper;
import com.yuncheng.spcyApi.service.ISpcyJcyJpjbxxService;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 检查员解聘基本信息
 * @Author: yuncheng
 * @Date: 2024-01-25
 * @Version: V1.0
 */
@Service
public class SpcyJcyJpjbxxServiceImpl extends BaseServiceImpl<SpcyJcyJpjbxxMapper, SpcyJcyJpjbxx> implements ISpcyJcyJpjbxxService {

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public Page<SpcyJcyJpjbxx> pageList(SpcyJcyJpjbxx query, String gjz, PageVo pageVo, OrderItemVo orderItemVo) {
        Page<SpcyJcyJpjbxx> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<SpcyJcyJpjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFjpwh()), SpcyJcyJpjbxx::getFjpwh, query.getFjpwh());
        queryWrapper.like(StringUtils.isNotBlank(query.getFjpzt()), SpcyJcyJpjbxx::getFjpzt, query.getFjpzt());

        if (StringUtils.isNotBlank(gjz)){
            queryWrapper
                    .and(f->f.like(SpcyJcyJpjbxx::getFjpwh, gjz)
                            .or()
                            .like(SpcyJcyJpjbxx::getFjpzt, gjz)
                            .or()
                            .like(SpcyJcyJpjbxx::getFjply, gjz)
                    );
        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByDesc(SpcyJcyJpjbxx::getFjpdjsj);
            queryWrapper.orderByDesc(SpcyJcyJpjbxx::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return page(page, queryWrapper);
    }

    @Override
    public SpcyJcyJpjbxx addJpjbxx(SpcyJcyJpjbxx bean) {
        if (StringUtils.isBlank(bean.getFjpdjsj())){
            bean.setFjpdjsj(DateConversionUtils.DateToStringYYYYMMDD(new Date()));
        }

        if (StringUtils.isBlank(bean.getId())){
            bean.setId(GetUuIdUtils.ReplaceUuId());
        }

        bean.setFjpzt(JcyPrdjConstant.JPDJ_JPZT_JPDJ);
        if (bean.getFjprs() == null) {
            bean.setFjprs(0);
        }

        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        save(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_JCY_JPDJGL,operContent,bean.getId(), "添加解聘登记", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updateJpjbxx(SpcyJcyJpjbxx buffer) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String id = buffer.getId();

        SpcyJcyJpjbxx updateBean = getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCY_JPDJGL,operContent,id,"修改解聘登记", SpcyConstant.LOGS_SYS_TYPE_XG);

    }

    @Override
    public SpcyJcyJpjbxx submitJpjbxx(SpcyJcyJpjbxx updateBean, SpcyJcyJpjbxx buffer) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));

        updateBean.setFjpzt(JcyPrdjConstant.JPDJ_JPZT_JPWC);
        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCY_JPDJGL,operContent,updateBean.getId(),"修改解聘登记", SpcyConstant.LOGS_SYS_TYPE_XG);

        return updateBean;
    }

    @Override
    public List<String> deleteJpjbxx(List<String> idsList) {
        if (CollectionUtil.isEmpty(idsList)){
            throw new BusinessException("参数为空！");
        }

        List<String> tyDelIdList = new ArrayList<>(); // 允许删除的数据id

        List<SpcyJcyJpjbxx> list = listByIds(idsList);
        if (CollectionUtil.isNotEmpty(list)){
            for (SpcyJcyJpjbxx bean : list){

                if (StringUtils.isNotBlank(bean.getFjpzt()) && bean.getFjpzt().equals(JcyPrdjConstant.JPDJ_JPZT_JPWC)){
                    continue;
                }else {
                    tyDelIdList.add(bean.getId());

                    removeById(bean.getId());

                    String operContent = "删除信息：";
                    operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
                    spcyLogService.addLogs(LogConstant.LOGS_JCY_JPDJGL, operContent, bean.getId(), "删除解聘登记", SpcyConstant.LOGS_SYS_TYPE_SC);
                }
            }
        }

        return tyDelIdList;
    }
}
